﻿using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;
using System;
using ASPNETMVCApplication.Code.Filters;
using ASPNETMVCApplication.Repositories;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Code.HtmlHelpers;
using ASPNETMVCApplication.Controllers;
using ASPNETWebApplication;
using ASPNETMVCApplication.Areas.Supp.Models;
using ASPNETMVCApplication.Areas.Shop.Models;

namespace ASPNETMVCApplication.Areas.Supp.Controllers
{
    public class TicketController : Controller
    {
        private ICategoryRepository _categoryRepository;
        private IProductRepository _productRepository;
        private ITrafficRepository _trafficRepository;
        private IOrderRepository _orderRepository;
        private IPointRepository _pointRepository;
        public TicketController()
            : this(new CategoryRepository(), new ProductRepository(), new TrafficRepository(),new OrderRepository(),new PointRepository())
        {
        }

        public TicketController(ICategoryRepository categoryRepository, IProductRepository productRepository, ITrafficRepository trafficRepository, IOrderRepository orderRepository,IPointRepository pointRepository)
        {
            _categoryRepository = categoryRepository;
            _productRepository = productRepository;
            _trafficRepository = trafficRepository;
            _orderRepository = orderRepository;
            _pointRepository = pointRepository;
        }
        // GET: /Supp/Ticket/

        public ActionResult TicketPost()
        {
            List<Traffic> listTraffic = _trafficRepository.GetList(null).ToList();
            List<SelectListItem> listItem = new List<SelectListItem>();
            foreach (Traffic traffic in listTraffic)
            {
                listItem.Add(new SelectListItem { Text = traffic.TrafficName, Selected = false, Value = traffic.TrafficId.ToString() });
            }

           
            ViewData["ListTraffic"] = listItem;
            
            return View();
        }

        //
        public void GetAllPoint(int id)
        {

            List<Point> listPoint = _pointRepository.GetList(null).ToList();
            List<SelectListItem> listItemPoint = new List<SelectListItem>();
            foreach (Point point in listPoint)
            {
                listItemPoint.Add(new SelectListItem { Text = point.Description, Selected = false, Value = point.PointId.ToString() });
            }
            ViewData["ListPoint"] = listItemPoint;
        }

        //register account supplier
        [HttpPost]
        public ActionResult TicketPost(CategoryModel categoryModel)
        {
            int suppID = (int)Session["suppId"];
            int trafficID = categoryModel.TrafficID;
            categoryModel.SupplierID = suppID;
            var category = ASPNETMVCApplication.Areas.Supp.Models.Mapper.FromModel(categoryModel);
            category.Version = "0";
            int cateID = _categoryRepository.PostCategory(category);
            //int cateID = category.CategoryId;
            //var categorytemp = _categoryRepository.; 

            var traffic = _trafficRepository.Get(suppID, trafficID);
            var layer = traffic.Layer;
            var l1RowNumber = traffic.L1RowNumber;
            var l1NCPR = traffic.L1NCPR;
            var l2RowNumber = traffic.L2RowNumber;
            var l2NCPR = traffic.L2NCPR;

            var Pr = new Product();
            Pr.CategoryId = cateID;
            Pr.Status = 0;
            Pr.UnitPrice = 0;
            Pr.Description = "L1";
            if (layer == 1)
            {
                for (var i = 1; i <= l1RowNumber * l1NCPR; i++)
                {
                    Pr.ChairNumber = i;
                    _productRepository.Insert(Pr);
                }
            }


            return View(categoryModel);
        }
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SuppGetOrderList(OrderModel orderModel)
        {
            orderModel.SupplierId = (int)Session["suppId"];
            if (orderModel.DateFrom == null) orderModel.DateFrom = DateTime.Now;
            if (orderModel.DateThru == null) orderModel.DateThru = DateTime.Now;
            var orderList = _orderRepository.GetOrderList(orderModel.CustomerId, orderModel.SupplierId, orderModel.DateFrom, orderModel.DateThru, orderModel.Status);
            return View(orderList);
        }

        [HttpPost]
        public ActionResult SuppGetOrderDetail(int orderID)
        {
            
            return View();
        }

        [HttpPost]
        private void OrderApprove(int supplierID, int customerID, string customerName, int orderId, List<Product> NotApproveProductList, List<Product> ApproveProductList)
        {
            
            //OrderDetail[] arrayDetail = null;
            List<int> ChairList = new List<int>();
            List<string> ChairNameList = new List<string>();
            List<int> StatusList = new List<int>();
            //cập nhật trạng thái vé
            if (NotApproveProductList.Count > 0)
            {
                for (int i = 0; i < NotApproveProductList.Count; i++)
                {
                    NotApproveProductList[i].Status = 0;
                    _productRepository.Update(NotApproveProductList[i]);
                }
            }

            if (ApproveProductList.Count > 0)
            {
                for (int j = 0; j < NotApproveProductList.Count; j++)
                {
                    ChairList.Add(ApproveProductList[j].ProductId);
                }
            }

            Order orderHeader = new Order();
            orderHeader.CustomerId = customerID;
            orderHeader.SupplierId = supplierID;
            orderHeader.OrderDate = DateTime.Now;
            orderHeader.RequiredDate = DateTime.Now;
            orderHeader.Freight = 0;
            orderHeader.Status = 1;
            orderHeader.OrderDetails = null;

            _orderRepository.UpdateOrder(orderHeader, ChairList);

            //gửi mail thông báo tới khách hàng, nhà xe, sales center
        }
    
    
    
    }
}
